<template>
  <el-dialog title="关联音频配置" v-model="visible" width="1200px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="88px"
      >
        <el-form-item label="名字" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入音频的名字"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="标签名" prop="tagIdArray">
          <el-input
            v-model="queryParams.tagName"
            placeholder="请输入标签名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="关键字" prop="keyWordIdArray">
          <el-input
            v-model="queryParams.keyWord"
            placeholder="请输入关键字"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="id" width="80px" align="center" prop="id" />
        <el-table-column label="名字" align="center" prop="name" />
        <el-table-column label="描述" align="center" prop="textDesc" />
        <el-table-column label="音频文件" align="center" prop="fileId" >
          <template #default="scope">
            <span v-if="scope.row.fileId">
              <a style="text-decoration:none;" class="play-btn" @click="playFile(scope.row)" href="javascript:void(0)">
                播放
              </a>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="标签" align="center" prop="tagIdArray" >
          <template #default="scope">
            <span :key="item.id" v-for="(item,index) in scope.row.tagIdArray">
              {{item.tagName}}{{index!=scope.row.tagIdArray.length-1?'，':''}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="关键字" align="center" prop="keyWordIdArray" >
          <template #default="scope">
            <span :key="item.id" v-for="(item,index) in scope.row.keyWordIdArray">
              {{item.keyWord}}{{index!=scope.row.keyWordIdArray.length-1?'，':''}}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button :type="scope.row.selected ? 'info' : 'primary'" @click="toggle(scope.row)" size="small">{{scope.row.selected ? "取消选择" : "选择"}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <div class="btn-group" style="text-align:center;margin-top:24px;">
      <el-button @click="close">取消</el-button>
      <el-button @click="submit" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { AudioApi, AudioVO } from '@/api/lm/audio'
import {useAppStore} from "@/store/modules/app";

const emit = defineEmits("submit");
/** 音频 列表 */
defineOptions({ name: 'AudioChooser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const appStore = useAppStore()
const loading = ref(true) // 列表的加载中
const visible = ref(false) // 列表的加载中
const list = ref<AudioVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const choosedAudio = ref(null);
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  tagName: undefined,
  keyWord: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AudioApi.getAudioPage(queryParams)
    list.value = data.list
    total.value = data.total
    if(choosedAudio.value) {
      choosedAudio.value = list.value.find((item:any)=> {
        return choosedAudio.value.id === e.id;
      })
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

function toggle(record) {
  record.selected = !record.selected
  if(!record.selected) {
    choosedAudio.value = null;
  } else {
    if(choosedAudio.value) {
      choosedAudio.value.selected = false;
    }
    choosedAudio.value = record;
  }
}

function playFile(item) {
  appStore.getSimpleAudioPlayer.playAudio(item.fileUrl);
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.tagName = undefined;
  queryParams.keyWord = undefined;
  queryFormRef.value.resetFields();
  handleQuery()
}

function close() {
  visible.value = false;
  choosedAudio.value = null;
}

function submit() {
  emit("submit", choosedAudio.value);
  visible.value = false;
}

defineExpose({
  open: (options:any = {})=> {
    choosedAudio.value = options.audio;
    visible.value = true;

    queryParams.pageNo = 1;
    getList();
  }
})
</script>
